package de.toolschmied.jjas.client.widgets.toolbar;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CustomButton;
import com.google.gwt.user.client.ui.Image;

public class ToolbarButton extends CustomButton {
	
	public static final String STYLE_NAME = "toolbarButton";

	private final Action action;
	
	public ToolbarButton(Action action) {
		this.action = action;
		initButton();
	}

	Action getAction() {
		return action;
	}
	
	public void setDown(boolean down) {
		super.setDown(down);
	}
	
	public boolean isDown() {
		return super.isDown();
	}
	
	private void initButton() {
		setStyle();
		setImage();
		setToolTip();
		addSelectionListener();
	}

	private void setStyle() {
		setStylePrimaryName(STYLE_NAME);
	}

	private void setImage() {
		String imageURL = action.getImageURL();
		Image image = new Image(imageURL);
		getUpFace().setImage(image);
		//getDownFace().setImage(image);
	}

	private void setToolTip() {
		// TODO: add a tool tip
	}

	private void addSelectionListener() {
		addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireAction();
			}
		});
	}
	
	private void fireAction() {
		action.run();
	}
	
}
